<html>
<head>
</head>
<body>
	<h1>WebSocket Test</h1>
	<form>
		<p>
			Username: <input id="username" type="text" value="">
		</p>
		<p>
			Message: <input id="message" type="text" value="Hello, world!">
		</p>
	</form>
	<button onclick="send();">Send Message</button>
	<div id="content"></div>

	<script type="text/javascript">
		var sock = null;

		window.onload = function () {
			sock = new WebSocket("ws://127.0.0.1:1234/soso");

			sock.onopen = function () {
				console.log("connected");
			}

			sock.onclose = function (e) {
				console.log("connection closed (" + e.code + ")");
			}

			sock.onmessage = function (e) {
				console.log("message received: " + e.data);
				var data = JSON.parse(e.data);
				var content = document.getElementById('content');
				var node = document.createElement('div');
				node.innerText = data.fromUser + ": " + data.body;
				content.appendChild(node);
			}
		};

		function send() {
			var username = document.getElementById('username').value;
			if (!username) {
				alert('请输入名称');
				return
			}
			var msg = document.getElementById('message').value;
			sock.send(JSON.stringify({fromUser: username, body: msg}));
		};
	</script>
</body>
</html>